import React, { Component } from "react";
// ant
import { Modal, message } from "antd";
// API
import { serviceAdd } from "@/api/raycloud";
// 组件
import FormCom from "@c/form/Index";
// Store
import Store from "@/stroe/Index";


class ServiceModal extends Component {
    constructor(props){
        super(props);
        this.state = {
            isModalVisible: false,
            user_id: "",
            formConfig: {
                url: "serviceAdd",
                editKey: "",
                initValue: {
                    number: 0,
                    status: "",
                    parentId: ""
                },
                setFieldValue: {},
                formatFormKey: "id"
            },
            formLayout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 20 }
            },
            formItem: [
                { 
                    type: "Input",
                    label: "服务名称", 
                    name: "name", 
                    required: true, 
                    style: { width: "200px" , marginLeft: "20px"},
                    placeholder: "请输入服务名称"
                },
                { 
                    type: "Input",
                    label: "服务代号", 
                    name: "code", 
                    required: true,
                    style: { width: "200px" , marginLeft: "20px" },
                    placeholder: "请输入服务代号",
                }
            ]
        };
    }

    componentDidMount(){
        this.props.onRef(this);
    }

    onFormRef = (ref) => {
        this.child = ref;
    }

    // 关闭弹窗
    handleCancel = () => {
        // 清单表单
        this.child.onReset();
        this.setState({
            isModalVisible: false,  // 关闭弹窗
            formConfig: { setFieldValue: "" },  // 清除form的初始数据
        })
    }
    
    /** 弹窗 */
    visibleModal = (params) => {
        this.setState({
            isModalVisible: params.status,
        })
    }

    onBlurEvent = (e) => {
        const value = e.currentTarget.value;
        if(value) {
            this.updateItem(value ? false : true);
            return false;
        }
    }
    // 提交按钮
    submit = (value) => {
        this.state.user_id ? this.handlerFormEdit(value) : this.handlerFormAdd(value);
        
    }
    // 添加 提交表单
    handlerFormAdd= (value) => {
        const requestData = value;
        requestData.department_code = Store.getState().app['department'];
        console.log(requestData)
        serviceAdd(requestData).then(response => {
            const responseData = response.data;
            // 提示
            message.info(responseData.msg);
            // 关闭弹准备
            this.handleCancel(false);
        })
    }
    // 编辑 提交表单
    handlerFormEdit= (value) => {
        console.log(value)
    }

    render(){
        return (
            <Modal title="添加服务" visible={this.state.isModalVisible} footer={null} onCancel={this.handleCancel}>
                <FormCom onRef={this.onFormRef} onBlur={this.onBlurEvent} formItem={this.state.formItem} formLayout={this.state.formLayout} formConfig={this.state.formConfig} submit={this.submit}>
                </FormCom>
            </Modal>
        )
    }
}

export default ServiceModal;